<?php

namespace Lib;

class Database{

    static $connection = NULL;
    
    static function getConnection(){
        if(is_null(self::$connection)){
            $connect = \Lib\Registry::get('config')->get('db');
            self::$connection = new \PDO("mysql:host=".$connect['host'].";dbname=".$connect['dbname'].";", $connect['user'], $connect['password']);
            self::$connection->query('set names utf8');
        }
        return self::$connection;
    }

    static function select($query){
        return self::$connection->query($query)->fetch(\PDO::FETCH_ASSOC);

    }

    static function selectAll($query){
        return  self::$connection->query($query)->fetchAll(\PDO::FETCH_ASSOC);
    }

    static function exec($query){
        return Database::$connection->exec($query);
    }

    static function getLastInsertId(){
        return Database::$connection->lastInsertId();
    }

    static function escapeString($string){
        return htmlspecialchars($string);
    }
}
?>
